package com.video.main_video.webConfig;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.video.main_video.common.cutException.ResponseException;
import com.video.main_video.common.cutException.ResponseExceptionEnum;
import com.video.main_video.common.jwt.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Optional;

@Component
public class RequestHeaderFilter implements HandlerInterceptor {


//    @Autowired
//    RedisTemplate<String,Object> fqRedisTemplate;
//    @Autowired
//    RedisServiceImp redisServiceImp;
    @Override
    public boolean preHandle(
            HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("Authorization");
        String requestURI = request.getRequestURI();
        Optional.ofNullable(token).orElseThrow(() -> new ResponseException(ResponseExceptionEnum.TOKEN_INCORRECT, "TOKEN校验错误"));
        DecodedJWT verify = JwtUtils.verify(token);
        Optional.ofNullable(verify).orElseThrow(() -> new ResponseException(ResponseExceptionEnum.TOKEN_INCORRECT, "TOKEN已过期"));
//        String redisToken = redisServiceImp.getValue("admin");
        System.out.println("验证");
        System.out.println(request.getRequestURI());
        String redisToken = null;
        return true;//返回false就不会走下去
    }

    @Override
    public void postHandle(
            HttpServletRequest request, HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
                                Object handler, Exception exception) throws Exception {
    }


}